package com.example.qx.adapter;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.AndroidException;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.qx.R;
import com.example.qx.database.data;
import com.example.qx.faxiana.qxq;
import com.example.qx.lei.cos;
import com.example.qx.lei.itemqxq;
import com.example.qx.lei.tianjianqxq;
import com.example.qx.zy;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.video.PlaceholderSurface;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.sql.SQLException;
import java.util.List;
import java.util.Objects;

public class qxqadapter extends RecyclerView.Adapter<qxqadapter.qxqHolder> {
    private List<itemqxq> itemlist;
    private tianjianqxq tiajianqxq=tianjianqxq.getInstance();
    private Context context;
    private data data=new data();
    public qxqadapter(List<itemqxq> itemlist,Context context) throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException {

        this.itemlist=itemlist;
        this.context=context;
    }
    @NonNull
    @Override
    public qxqHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.itemqxq,parent,false);
        return  new qxqHolder(view);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void onBindViewHolder(@NonNull qxqHolder holder, int position) {
        cos cos=new cos();
        itemqxq itemqxq=itemlist.get(position);
        holder.xiangfa.setText(itemqxq.getXiangfa());
        holder.mingzia.setText(itemqxq.getMingzi());
        if(!Objects.equals(itemqxq.getTouxiang(), "s")&&(!Objects.equals(itemqxq.getTouxiang(), "")))
         {
             String b=String.valueOf(itemqxq.getId());
                     b=b+"tx";
                 File filee=context.getDir("youqishuju",context.MODE_PRIVATE);
                 String abc= filee.getAbsolutePath();
                 File filel=new File(abc+"/"+b);
                 if(!filel.exists())
                     cos.get(b,context);
                 Bitmap bitmap =BitmapFactory.decodeFile(abc+"/"+b);
                 holder.touxianga.setImageBitmap(bitmap);



         }
        if(!Objects.equals(itemqxq.getUri(), "s")&&(!Objects.equals(itemqxq.getUri(), "")))
        {
            String b=String.valueOf(itemqxq.getId());
            b=b+"qxq";
            String c=String.valueOf(itemqxq.getSum());
            b=b+c;
            String d="a"+b;
            File filee=context.getDir("youqishuju",context.MODE_PRIVATE);
            String abc= filee.getAbsolutePath();
            File filel=new File(abc+"/"+b);
            File filell=new File(abc+"/"+d);
            if(!filel.exists()&&!filell.exists()) {
                cos.get(b, context);
                cos.get(d,context);
            }
            if(filell.exists()){
                 Bitmap bitmap =BitmapFactory.decodeFile(abc+"/"+d);
                   holder.imageView.setImageBitmap(bitmap);

                   final boolean tupian[]={false};
                holder.imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if(!tupian[0])
                        {   holder.butsc.setVisibility(View.GONE);
                            holder.xiangfa.setVisibility(View.GONE);
                            ConstraintLayout.LayoutParams params2=(ConstraintLayout.LayoutParams) holder.imageView.getLayoutParams();
                            params2.height=ViewGroup.LayoutParams.MATCH_PARENT;
                            params2.width=ViewGroup.LayoutParams.MATCH_PARENT;
                            tupian[0]=true;
                        }
                        else{
                               holder.butsc.setVisibility(View.VISIBLE);
                               holder.xiangfa.setVisibility(View.VISIBLE);
                            ConstraintLayout.LayoutParams params3=(ConstraintLayout.LayoutParams) holder.imageView.getLayoutParams();
                            params3.width=(int) (250*context.getResources().getDisplayMetrics().density);
                            params3.height=(int) (150*context.getResources().getDisplayMetrics().density);
                            tupian[0]=false;
                        }

                    }
                });



            }
            else if(filel.exists()) {
                       holder.playerView.setVisibility(View.VISIBLE);
                    ExoPlayer exoPlayer = new ExoPlayer.Builder(context).build();
                    holder.playerView.setPlayer(exoPlayer);
                    MediaItem mediaItem= MediaItem.fromUri(abc+"/"+b);
                    exoPlayer.setMediaItem(mediaItem);
                    exoPlayer.prepare();
                final boolean[] isfull = {false};
                final boolean[] kzq = {true};

                GestureDetector gestureDetector=new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){
                    @Override
                    public boolean onDoubleTap(MotionEvent e){
                        if(!isfull[0]){
                            holder.butsc.setVisibility(View.GONE);
                            holder.xiangfa.setVisibility(View.GONE);
                            ConstraintLayout.LayoutParams params1=(ConstraintLayout.LayoutParams) holder.playerView.getLayoutParams();
                            params1.width=ViewGroup.LayoutParams.MATCH_PARENT;
                            params1.height=ViewGroup.LayoutParams.MATCH_PARENT;
                            holder.playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FILL);
                            isfull[0] =true;

                        }
                        else {
                            holder.butsc.setVisibility(View.VISIBLE);
                            holder.xiangfa.setVisibility(View.VISIBLE);
                            ConstraintLayout.LayoutParams params=(ConstraintLayout.LayoutParams)holder.playerView.getLayoutParams();
                            params.width=(int) (250*context.getResources().getDisplayMetrics().density);
                            params.height=(int) (150*context.getResources().getDisplayMetrics().density);
                            holder.playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIT);
                            isfull[0]=false;
                        }
                        return true;
                    }
                    @Override
                    public boolean onSingleTapConfirmed(MotionEvent e){
                        if(kzq[0])
                        {holder.playerView.hideController();
                            kzq[0]=false;
                        }
                        else {
                           holder.playerView.showController();
                            kzq[0]=true;
                        }

                        return true;

                    }
                });
                holder.playerView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        gestureDetector.onTouchEvent(event);
                        return true;
                    }
                });

            }}


            holder.butsc.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        String b=String.valueOf(itemqxq.getId());
                        b=b+"qxq";
                        String c=String.valueOf(itemqxq.getSum());
                        b=b+c;
                        String d="a"+b;
                        data.deleteqxq(itemqxq.getId(),itemqxq.getSum());
                        File file=context.getDir("youqishuju",context.MODE_PRIVATE);
                        File fiell=new File(file,b);
                        File fielll=new File(file,d);
                        if(fiell.exists()) {
                            fiell.delete();
                            cos.delete(b,context);
                        }
                        if(fielll.exists()) {
                            fielll.delete();
                            cos.delete(d,context);
                        }

                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                    Intent intent=new Intent(context, qxq.class);
                    context.startActivity(intent);
                }
            });


    }


    @Override
    public int getItemCount() {
        return itemlist.size();
    }
    public static class qxqHolder extends RecyclerView.ViewHolder{
        private TextView xiangfa,mingzia;
        private ImageView imageView,touxianga;
        private PlayerView playerView;
        private  Button butsc;
        private ViewPager viewPager;


        public qxqHolder(@Nullable View v){
            super(v);
            xiangfa=v.findViewById(R.id.tianjiaxf);
            imageView=v.findViewById(R.id.fabiaotupian);
            playerView=v.findViewById(R.id.fabiaoshipin);
            butsc=v.findViewById(R.id.qxqsc);
            viewPager=v.findViewById(R.id.viewpager);
            touxianga=v.findViewById(R.id.touxianga);
            mingzia=v.findViewById(R.id.mingzia);
        }


    }
}
